#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <semaphore.h>
#include <signal.h>
struct Job
{
    void (*task)(void *arg); // 指向线程需要执行的具体任务
    void *arg;               // 线程传的参数

    struct Job *next;
};

struct thread_pool
{
    int thread_num;     // 线程池的线程数量
    int queue_max_size; // 任务队列的最大值
    int queue_cur_size; // 任务队列的当前任务的个数

    pthread_t *ids;                 // 保存线程池中线程的id
    pthread_mutex_t mutex;          // 线程池的锁
    pthread_cond_t empty_queue;     // 等待任务队列空的条件
    pthread_cond_t not_empty_queue; // 等待任务队列非空
    pthread_cond_t not_full_queue;  // 等待任务队列非满队

    struct Job *font;
    struct Job *real;

    int pool_close;  // 线程池关闭：0  线程池运行：1
    int queue_close; // 任务队列是否可以操作的标志位；
};
struct thread_pool *Create_pool(int thread_num, int queue_max_size)
{
    struct thread_pool *pool = (struct thread_pool *)malloc(sizeof(struct thread_pool));
    if (pool == NULL)
    {
        printf("pool malloc is error!\n");
        return NULL;
    }

    pool->thread_num = thread_num;
    pool->queue_max_size = queue_max_size;
    pool->queue_cur_size = 0;

    if (pthread_mutex_init(&(pool->mutex), NULL) != 0)
    {
        perror("thread pool mutex init error!");
        goto err;
    }

    if (pthread_cond_init(&(pool->not_empty_queue), NULL) != 0)
    {
        perror("thread pool cond not empty queue init error!");
        goto err;
    }

    if (pthread_cond_init(&(pool->not_full_queue), NULL) != 0)
    {
        perror("thread pool cond not full queue init error!");
        goto err;
    }

    if (pthread_cond_init(&(pool->empty_queue), NULL) != 0)
    {
        perror("thread pool cond empty queue init error!");
        goto err;
    }

    pool->font = NULL;
    pool->real = NULL;

    pool->queue_close = 0;
    pool->pool_close = 0;

    pool->ids = (pthread_t *)malloc(sizeof(pthread_t) * thread_num);
    if (pool->ids == NULL)
    {
        printf("pthread ids malloc error!\n");
        goto err;
    }
    memset(pool->ids, 0, sizeof(pthread_t) * thread_num);

    for (int i = 0; i < thread_num; i++)
    {
        if (pthread_create(&(pool->ids[i]), NULL, thread_function, (void *)pool) < 0)
        {
            perror("thread create error!");
            goto err;
        }
    }

    return pool;
err:
    free(pool);
    pool = NULL;
    return pool;
}
void thread_function(void *arg)
{
    struct thread_pool *pool = (struct thread_pool *)arg;
    pthread_mutex_lock(&pool->mutex);
    while (1)
    {
        if (pool->queue_close == 0 || pool->pool_close == 0)
        {
            printf("queue empty\n");
            pthread_mutex_lock(&pool->mutex);
            pthread_exit(NULL);
        }
        while ((pool->queue_cur_size == 0) && (pool->queue_close != 1))
        {
            pthread_cond_wait(&(pool->not_empty_queue), &(pool->mutex));
        }
        // 从任务队列中取任务;
        struct Job *job = pool->font;
        pool->font = pool->font->next;
        pool->queue_cur_size--;
        if(pool->queue_cur_size==0)
        {
            pthread_cond_broadcast(&pool->empty_queue);
            
        }
    }
}